package com.hundredapp.oa.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

public class DateConverter extends StrutsTypeConverter {
	public final static String DATE_FORMAT = "yyyy-mm-dd";
	
	private SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
	@Override
	public Object convertFromString(Map map, String[] values, Class toType) {
		
		Date date = null;
		try {
			date = formatter.parse(values[0]);
		} catch (ParseException e) {
			e.printStackTrace();
		}
			
		if(toType == Date.class) {
			return date;
		} else if(toType == Timestamp.class)  {
			return new Timestamp(date.getTime());
		} else {
			return values[0];
		}

	}

	@Override
	public String convertToString(Map map, Object value) {
		return formatter.format(value);
	}
}
